package com.powernode.business.controller;

import com.powernode.business.constant.SnConstant;
import com.powernode.business.domain.Attachment;
import com.powernode.business.domain.Customer;
import com.powernode.business.domain.Rent;
import com.powernode.business.service.AttachmentService;
import com.powernode.business.service.CustomerService;
import com.powernode.business.service.RentService;
import com.powernode.business.utils.CreateSnUtil;
import com.powernode.business.vo.RentVo;
import com.powernode.common.core.controller.BaseController;
import com.powernode.common.core.domain.AjaxResult;
import com.powernode.common.core.domain.model.LoginUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/business/rentCar")
@Api(tags = "租车管理")
public class RentCarController extends BaseController {

    @Autowired
    private CustomerService customerService;

    @Autowired
    private AttachmentService attachmentService;

    @Autowired
    private CreateSnUtil createSnUtil;

    @Autowired
    private RentService rentService;

    /**
     * 检验用户是否存在
     * @param identity
     * @return
     */
    @GetMapping("checkIdentity")
    @ApiOperation("检验用户身份是否存在")
    public AjaxResult checkCustomerId(String identity){
        Customer customer = customerService.selectByIdentity(identity);
        if (ObjectUtils.isEmpty(customer)){
            return AjaxResult.error("客户未注册");
        }
        return AjaxResult.success();
    }

    @GetMapping("showCarImgs")
    @ApiOperation("获取当前车辆大图的集合数组")
    public AjaxResult showCarImgs(String carNumber){
        List<String> imgs = attachmentService.showCarImgs(carNumber);
        if (CollectionUtils.isEmpty(imgs)){
            return AjaxResult.error("该车辆没有图片");
        }
        return AjaxResult.success(imgs);
    }


    @GetMapping("toRentCar")
    @ApiOperation("生成出租单号")
    public AjaxResult toRentCar(){
        //获取当前用户
        LoginUser loginUser = getLoginUser();
        String rentSn = createSnUtil.createSn(SnConstant.RENT_SN_PREFIX);
        RentVo rentVo = new RentVo(rentSn,loginUser.getUsername());
        //Java最好不要用动态类型 map list 最好用对象
        return AjaxResult.success(rentVo);
    }

    //租车就是汪rent表中插一条记录，修改车辆状态

    @PostMapping("doRentCar")
    @ApiOperation("租车")
    public AjaxResult doRentCar(@RequestBody Rent rent){
        //门店的业务员只能租该门店的车
        int i = rentService.realDoRent(rent);
        if (i > 0){
            return AjaxResult.success("出租成功");
        }
        return AjaxResult.error("服务器异常,请联系管理员");
    }
}
